package xyz.xtt.exchange.controller;

import java.time.LocalDateTime;
import java.util.Enumeration;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.exchange.entity.TblAsset;
import xyz.xtt.exchange.entity.TblAssetExt;
import xyz.xtt.exchange.entity.TblAssetStaticResource;
import xyz.xtt.exchange.entity.TblUserChainAccount;
import xyz.xtt.exchange.mapper.TblAssetMapper;
import xyz.xtt.exchange.mapper.TblUserChainAccountMapper;
import xyz.xtt.exchange.vo.AssetObject;
import xyz.xtt.exchange.vo.AssetObject2;
import xyz.xtt.exchange.vo.AssetVo;

/**
 * @author huqibo
 *
 */
@Slf4j
@RequestMapping("/api/exchange")
@RestController
public class IndexController {
	@Autowired
	private TblAssetMapper tblAssetMapper;
	@Autowired
	private TblUserChainAccountMapper tlUserChainAccountMapper;

	@GetMapping("/index")
	public ResponseEntity<String> index(HttpServletRequest request, @RequestHeader("uid") String uid) {
		Enumeration<String> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String nextElement = headerNames.nextElement();
			log.info("{}: {}", nextElement, request.getHeader(nextElement));
		}
		return ResponseEntity.ok(UUID.randomUUID().toString() + " | " + uid);
	}

	@GetMapping("/kafka")
	public ResponseEntity<String> kafka(HttpServletRequest request, @RequestHeader("uid") String uid) {
		Enumeration<String> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String nextElement = headerNames.nextElement();
			log.info("{}: {}", nextElement, request.getHeader(nextElement));
		}
		return ResponseEntity.ok(UUID.randomUUID().toString() + " | " + uid);
	}

	@GetMapping("/join")
	public ResponseEntity<List<AssetVo>> join() {
		MPJLambdaWrapper<TblAsset> leftJoin = JoinWrappers.lambda(TblAsset.class)
//			.select(TblAsset::getAssetNo, TblAsset::getAssetName)
				.selectAll(TblAsset.class)
				.select(TblAssetExt::getSellStartDatetime)
				.select(TblAssetStaticResource::getLastModifyTime, TblAssetStaticResource::getLogoUri, TblAssetStaticResource::getDisplayUri, TblAssetStaticResource::getFileUri)
				.leftJoin(TblAssetExt.class, TblAssetExt::getAssetId, TblAsset::getId)
				.leftJoin(TblAssetStaticResource.class, TblAssetStaticResource::getAssetId, TblAsset::getId)
				.eq(TblAsset::getId, 1851892786240593920L);
		List<AssetVo> selectJoinList = tblAssetMapper.selectJoinList(AssetVo.class, leftJoin);
		return ResponseEntity.ok(selectJoinList);
	}

	@GetMapping("/query1")
	public ResponseEntity<List<AssetObject>> onetomore() {
		MPJLambdaWrapper<TblAsset> leftJoin = JoinWrappers
				.lambda(TblAsset.class)
				.selectCollection(TblAssetExt.class, AssetObject2::getAssetExt)
				.selectCollection(TblAssetStaticResource.class, AssetObject2::getStaticResource)
				.leftJoin(TblAssetExt.class, TblAssetExt::getAssetId, TblAsset::getId)
				.leftJoin(TblAssetStaticResource.class, TblAssetStaticResource::getAssetId, TblAsset::getId)
				.eq(TblAsset::getId, 1851892786240593920L);
		List<AssetObject> selectJoinList = tblAssetMapper.selectJoinList(AssetObject.class, leftJoin);
		return ResponseEntity.ok(selectJoinList);
	}

	@GetMapping("/onetoone")
	public ResponseEntity<List<AssetObject>> onetoone() {
		MPJLambdaWrapper<TblAsset> leftJoin = JoinWrappers
				.lambda(TblAsset.class)
				.selectAssociation(TblAsset.class, AssetObject::getAsset)
				.selectAssociation(TblAssetExt.class, AssetObject::getAssetExt)
				.selectAssociation(TblAssetStaticResource.class, AssetObject::getStaticResource)
				.leftJoin(TblAssetExt.class, TblAssetExt::getAssetId, TblAsset::getId)
				.leftJoin(TblAssetStaticResource.class, TblAssetStaticResource::getAssetId, TblAsset::getId)
				.eq(TblAsset::getId, 1851892786240593920L);
		List<AssetObject> selectJoinList = tblAssetMapper.selectJoinList(AssetObject.class, leftJoin);
		return ResponseEntity.ok(selectJoinList);
	}

	@GetMapping("/query/list")
	public ResponseEntity<List<TblUserChainAccount>> list() {
		LambdaQueryChainWrapper<TblUserChainAccount> query = new LambdaQueryChainWrapper<>(tlUserChainAccountMapper);
		List<TblUserChainAccount> list = query.list();
//		LambdaUpdateChainWrapper up = new LambdaUpdateChainWrapper(tlUserChainAccountMapper);
//		up.update();
		return ResponseEntity.ok(list);
	}

	@GetMapping("/update/data")
	public ResponseEntity<Boolean> update() {
		LambdaUpdateChainWrapper<TblUserChainAccount> up = new LambdaUpdateChainWrapper<TblUserChainAccount>(tlUserChainAccountMapper);
		up.eq(TblUserChainAccount::getAppUserId, "0").set(TblUserChainAccount::getLastModifyDate, LocalDateTime.now());
		boolean update = up.update();
		return ResponseEntity.ok(update);
	}

	@GetMapping("/query/vec")
	public ResponseEntity<List<TblUserChainAccount>> vec() {

		LambdaQueryChainWrapper<TblUserChainAccount> query = new LambdaQueryChainWrapper<>(tlUserChainAccountMapper);
		List<TblUserChainAccount> list = query.list();
//		LambdaUpdateChainWrapper up = new LambdaUpdateChainWrapper(tlUserChainAccountMapper);
//		up.update();
		return ResponseEntity.ok(list);
	}
}
